package com.dhemery.mack.gui;

import org.eclipse.jface.viewers.TreeViewer;

import com.dhemery.mack.model.Folder;
import com.dhemery.mack.model.FolderChangeListener;
import com.dhemery.mack.model.Item;
import com.dhemery.mack.model.ItemChangeListener;
import com.dhemery.mack.model.Node;

//TODO Test this class
public class NodeChangeHandler implements ItemChangeListener, FolderChangeListener {

	private final TreeViewer viewer;

	public NodeChangeHandler(TreeViewer viewer) {
		this.viewer = viewer;
		subscribe();
	}
	
	public void nodeAdded(Folder folder, Node node) {
		viewer.refresh(folder);
		viewer.expandToLevel(folder, 1);
	}

	public void nodeRemoved(Folder folder, Node node) {
		viewer.refresh(folder);
	}

	public void nameChanged(Item item) {
		viewer.refresh(item);
	}

	public void subscribe() {
		Folder.subscribeToChildChange(this);
		Item.subscribeToNameChange(this);
	}

	public void unsubscribe() {
		Folder.unsubscribeFromChildChange(this);
		Item.unsubscribeFromNameChange(this);
	}

	public void dispose() {
		unsubscribe();
	}
}
